package com.techblor.wechat.library.api;

import com.alibaba.fastjson.JSON;
import com.techblor.wechat.library.bean.department.DepartmentResult;
import com.techblor.wechat.library.bean.department.UserSimpleListResult;
import com.techblor.wechat.library.bean.externalcontact.ExternalContactListResult;
import com.techblor.wechat.library.bean.externalcontact.ExternalContactResult;
import com.techblor.wechat.library.bean.token.WechatAccessToken;
import com.techblor.wechat.library.util.HttpClientUtil;

/**
 * 企业微信api
 */
public class Qyapi extends WechatApi {

    /**
     * 获取access_token
     *
     * @param corpid     企业ID
     * @param corpsecret 应用的凭证密钥
     * @return
     */
    public WechatAccessToken getToken(String corpid, String corpsecret) {
        String url = QYAPI_URI + "/cgi-bin/gettoken?corpid=" + corpid +
                "&corpsecret=" + corpsecret;

        String result = HttpClientUtil.doGet(url);
        return JSON.parseObject(result, WechatAccessToken.class);
    }

    /**
     * 获取部门列表
     *
     * @param access_token 调用接口凭证
     * @param id           部门id. 获取指定部门及其下的子部门(以及及子部门的子部门等等，递归) 如果不填, 默认获取全量组织架构
     * @return
     */
    public DepartmentResult departmentList(String access_token, String id) {
        String url = QYAPI_URI + "/cgi-bin/department/list?access_token=" + access_token +
                "&id=" + id;

        String result = HttpClientUtil.doGet(url);
        return JSON.parseObject(result, DepartmentResult.class);
    }

    /**
     * 获取部门成员
     *
     * @param access_token  调用接口凭证
     * @param department_id 获取的部门id
     * @param fetch_child   是否递归获取子部门下面的成员：1-递归获取，0-只获取本部门
     * @return
     */
    public UserSimpleListResult userSimpleList(String access_token, int department_id, int fetch_child) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + access_token +
                "&department_id=" + department_id +
                "&fetch_child=" + fetch_child;

        String result = HttpClientUtil.doGet(url);
        return JSON.parseObject(result, UserSimpleListResult.class);
    }

    /**
     * 获取客户列表
     *
     * @param access_token 调用接口凭证
     * @param userid       企业成员的userid
     * @return
     */
    public ExternalContactListResult externalContactList(String access_token, String userid) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list" +
                "?access_token=" + access_token +
                "&userid=" + userid;

        String result = HttpClientUtil.doGet(url);
        return JSON.parseObject(result, ExternalContactListResult.class);
    }

    /**
     * 获取客户详情
     *
     * @param access_token    调用接口凭证
     * @param external_userid 外部联系人的userid, 注意不是企业成员的帐号
     * @return
     */
    public ExternalContactResult getExternalContact(String access_token, String external_userid, String cursor) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get" +
                "?access_token=" + access_token +
                "&external_userid=" + external_userid +
                "&cursor=" + cursor;

        String result = HttpClientUtil.doGet(url);
        return JSON.parseObject(result, ExternalContactResult.class);
    }

}
